package com.zxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.common.ResultData;
import com.zxy.entity.Card;
import com.zxy.mapper.CardMapper;
import com.zxy.service.ICardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Zxy
 * @since 2021-05-11
 */
@Service
@AllArgsConstructor
public class CardServiceImpl extends ServiceImpl<CardMapper, Card> implements ICardService {

    private final CardMapper cardMapper;

    @Override
    public ResultData cardApply(String name) {
        // 检查一卡通表中是否已经存在，如果存在
        if (checkApply(name)) {
            Card card = new Card(null, name, 0);
            cardMapper.insert(card);
            return ResultData.success(card);
        } else {
            return ResultData.success("您已经申请了一卡通");
        }
    }


    @Override
    public Boolean checkApply(String name) {
        QueryWrapper<Card> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        Card card = cardMapper.selectOne(wrapper);
        // 如果查询到没有Card对象，则是没申请
        if (card == null) {
            return true;
        }
        return false;
    }
}
